package me.youline.dataServer.service.image;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;

import me.youline.dataServer.ConfigConstants;

import com.alibaba.simpleimage.ImageWrapper;
import com.google.common.collect.Lists;

/**
 * 按比例图像尺寸压缩上下文，此类会将原图按预先设定的比例创建需要略缩的尺寸
 * @author linhan
 *
 */
public class RatioImageScaleContext implements ImageScaleContext {

	/**
	 * 源文件 
	 */
	private File sourceFile;
	
	/**
	 * 略缩比例
	 */
	private static float RATION = 0.3F;
	
	/**
	 * 宽度
	 */
	private int width;
	
	/**
	 * 高度
	 */
	private int height;
	
	/**
	 * 文件名格式定义
	 */
	private static String FILE_NAME_FORMAT = "%s[%s]";
	
	public RatioImageScaleContext(File sourceFile) throws IOException {
		ImageWrapper w =new ImageWrapper(ImageIO.read(sourceFile));
		width = (int) (RATION * w.getWidth());
		height = (int) (RATION* w.getHeight());
		this.sourceFile = sourceFile;
	}

	@Override
	public File getSourceFile() {
		return this.sourceFile;
	}

	@Override
	public List<ImageSize> getScaleSizes() {
		return Lists.newArrayList(new ImageSize(height, width));
	}
	
	
	@Override
	public File getTargetFile(ImageSize size) {
		String path = sourceFile.getParent();
		String fileName = String.format(FILE_NAME_FORMAT,
				sourceFile.getName(), ImageSize.createThumbnailImageSize().getSizeFormatStr());
		return new File(path + ConfigConstants.PATH_SEPARATOR + fileName);
	}

}
